/***************************************************************************
 *   Copyright (C) 2008 by JBox systems
 *   gabi@shaargiora.co.il
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/

#include "modelitems.h"

ModelItem::ModelItem(ModelItem *parent):
	m_parent(parent) {
}

ModelItem::~ModelItem() {
	qDeleteAll(m_child_items);
}

void ModelItem::appendChild(ModelItem *item) {
	m_child_items.append(item);
}

int ModelItem::row() const {
	if (parent())
		return parent()->m_child_items.indexOf(const_cast<ModelItem*>(this));
	return 0;
}

bool ModelItem::isCheckable(int col) const {
	if (col < 0 || col >= m_cells.size())
		return false;
	return m_cells[col].checkable;
}

void ModelItem::setState(int col, bool st) {
	if (col < 0 || col >= m_cells.size())
		return;
	m_cells[col].state = st;
}

bool ModelItem::state(int col) const {
	if (col < 0 || col >= m_cells.size())
		return false;
	return m_cells[col].state;
}

QVariant ModelItem::data(int col) const {
	if (col < 0 || col >= m_cells.size())
		return QVariant();
	return QVariant(m_cells[col].text.c_str());
}

void ModelItem::reset() {
	for (int i=0; i < m_cells.size(); ++i)
		m_cells[i].state = false;
}

RootItem::RootItem(const QList<QVariant> &data, ModelItem *parent):
	ModelItem(parent),
	m_data(data) { }

AppItem::AppItem(Application app, ModelItem* parent) :
	ModelItem(parent),
	m_version(app.lastVersion()),
	m_application(&app) {
	m_version.is_last = true;
	m_cells << Cell(app.name()) << Cell("["+m_version.version()+"]", true) << Cell(app.summary()) << Cell(m_version.url()) << Cell();
	std::vector<Version> versions = app.versions();
	std::vector<Version>::iterator itr = versions.begin();
	while (itr != versions.end()) {
		VerItem *version = new VerItem(*itr, this);
		appendChild(static_cast<ModelItem*>(version));
		itr++;
	}
}

// QVariant AppItem::toolTip() const {
// 	return QVariant(QString(m_app->description().c_str()));
// }

VerItem::VerItem(Version version, ModelItem* parent) :
	ModelItem(parent),
	m_version(version) {
	m_cells << Cell() << Cell(version.version(), true) << Cell() << Cell(version.url()) << Cell(version.name());
}
